package ikelib;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Locale;


public class MapUtil {
	public static boolean showMap(double lat,double lon,String title)
	{
		
		title = title.replaceAll(" ", "%20");
		boolean ret = false;
		Locale loc = Locale.getDefault();
		
		// Google 
		String url = String.format("http://maps.google.co.jp/maps?q=%f,%f+(%s)&iwloc=A&z=10&t=m&om=1&hl=%s",lat,lon,title,loc.getLanguage());
		//String encoded = URLEncoder.encode(url, "UTF-8");
		// http://maps.google.co.jp/maps?q=35.358256,+138.677559+%28HWSH%29&iwloc=A&hl=en
		// http://maps.google.co.jp/maps?q=35.358256,+138.677559+%28HWSH%29&spn=0.004545,0.006839&iwloc=A&hl=ja&z=10&t=m&om=1
		// http://code.google.com/intl/ja/apis/maps/documentation/staticmaps/
		// http://mapki.com/wiki/Google_Map_Parameters
		// http://imakoko.didit.jp/imakoko_html/memo/parameters_google.html
        Desktop desktop = Desktop.getDesktop();
        try {
        	desktop.browse(new URI(url));
        	ret = true;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return ret;
	}
	//
	// Google Eearth .kml ファイルを作成してGoogle Earthを起動する
	// 
	public static boolean showEarth(ArrayList<Double> lat,ArrayList<Double> lon, ArrayList<Double> alt, ArrayList<String> title)
	{
		boolean ret = true;
		File f = new File("wichg.kml");
		FileWriter fw;
		try {
			fw = new FileWriter(f);
			BufferedWriter bw = new BufferedWriter(fw);
			PrintWriter pw = new PrintWriter(bw);
			
			pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			pw.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
			pw.println("<Document>");
			pw.println("<name>wichkg.document</name>");
			pw.println("<Folder>");
			
			
			for(int i=0;i<lat.size();i++){
				pw.println("<Placemark>");
				pw.println("<visibility>1</visibility>");
				pw.println(String.format("<name>%s</name>",title.get(i)));
				pw.println(String.format("<description>%s</description>",title.get(i)));
				pw.println("<Point>");
				pw.println(String.format("<coordinates>%f,%f,%f</coordinates>",lon.get(i),lat.get(i),alt.get(i)));
				pw.println("</Point>");
				pw.println("</Placemark>");
			}
			pw.println("</Folder>");
			pw.println("</Document>");
			pw.println("</kml>");
			pw.close();
			
			 Desktop desktop = Desktop.getDesktop();
			 desktop.open(f);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ret;
	}
	public static boolean showEarth(double lat,double lon, double alt, String title)
	{
		ArrayList<Double> lat2 = new ArrayList<Double>();
		ArrayList<Double> lon2 = new ArrayList<Double>();
		ArrayList<Double> alt2 = new ArrayList<Double>();
		ArrayList<String> tit2 = new ArrayList<String>();
		lat2.add(lat);
		lon2.add(lon);
		alt2.add(alt);
		tit2.add(title);
		return showEarth(lat2,lon2,alt2,tit2);
	}
}
